﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows;
namespace EzDesk.Generic.WPF.Converter
{
    public class StringToImageSource : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            //throw new NotImplementedException();
            if (value.GetType() != typeof(string))
                throw new Exception("Input object should be a string!");
            if (targetType != typeof(ImageSource))
                throw new Exception("Input object should be a string!");

            try
            {
                return BitmapFrame.Create(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));
            }
            catch (Exception exp)
            {
                return BitmapFrame.Create(new Uri("MessageIcon/20071221155744199.png", UriKind.RelativeOrAbsolute));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class XamlStringToFrameworkElement : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            if (value.GetType() != typeof(string))
                throw new Exception("Input object should be a string!");

            try
            {
                return System.Windows.Markup.XamlReader.Parse(value.ToString());
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DateTimeToString : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            if (value.GetType() != typeof(DateTime))
                throw new Exception("Input object should be a DateTime!");

            try
            {
                DateTime d = (DateTime)value;
                return d.ToFriendlyString();
            }
            catch (Exception exp)
            {
                return "Convert Error!";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}
